#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2025 Huawei Technologies Co., Ltd. All rights reserved.

from ansible.module_utils.basic import AnsibleModule

from openubmc_ansible.openubmc_redfish_api.redfish_base import BmcBaseConnect
from openubmc_ansible.openubmc_redfish_api.api_manage_boot_device import set_boot_device
from openubmc_ansible.openubmc_logger import report
from openubmc_ansible.openubmc_logger import log
from openubmc_ansible.utils import is_support_server
from openubmc_ansible.utils import ansible_bmc_run_module
from openubmc_ansible.utils import SERVERTYPE


ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    'status': ['preview'],
    'supported_by': 'community'
}

DOCUMENTATION = """
---
module: openubmc_set_boot_device
short_description: Set boot device
version_added: "2.5.0"
description:
    - "Modifying boot device information"
options:
  openubmc_ip:
    required: true
    default: None
    description:
      - openUBMC IP address
  openubmc_user:
    required: true
    default: None
    description:
      - openUBMC user name used for authentication
  openubmc_pswd:
    required: true
    default: None
    description:
      - openUBMC user password used for authentication
  boot_target:
    required: false
    default: None
    choices: [Cd, None, Pxe, Floppy, Hdd, BiosSetup]
    description:
      - Current boot device
  boot_enabled:
    required: false
    default: None
    choices: [Disabled, Once, Continuous]
    description:
      - Whether the boot settings are effective
  boot_mode:
    required: false
    default: None
    choices: [UEFI, Legacy]
    description:
      - Boot mode
"""

EXAMPLES = """
 - name: set boot device
    openubmc_set_boot_device:
      openubmc_ip: "{{ openubmc_ip }}"
      openubmc_user: "{{ openubmc_user }}"
      openubmc_pswd: "{{ openubmc_pswd }}"
      boot_target: "Cd"
      boot_enabled: "Once"
      boot_mode: "Legacy"
"""

RETURNS = """
    {"result": True, "msg": "Set boot device info successful!"}
"""


def openubmc_set_boot_device_module(module):
    """
    Function:
        Set boot device
    Args:
              module       (class):

    Returns:
        {"result": False, "msg": 'not run set boot device yet'}
    Raises:
        None
    Examples:

    Author:
    Date: 2019/11/4 17:33
    """
    with BmcBaseConnect(module.params, log, report) as openubmc:
        ret = is_support_server(openubmc, SERVERTYPE)
        if ret['result']:
            ret = set_boot_device(openubmc, module.params)
    return ret


def main():
    # Use AnsibleModule to read yml files and convert it to dict
    module = AnsibleModule(
        argument_spec={
            "openubmc_ip": {"required": True, "type": 'str'},
            "openubmc_user": {"required": True, "type": 'str'},
            "openubmc_pswd": {"required": True, "type": 'str', "no_log": True},
            "boot_target": {"required": False, "type": 'str'},
            "boot_enabled": {"required": False, "type": 'str'},
            "boot_mode": {"required": False, "type": 'str'}
        },
        supports_check_mode=False)

    ansible_bmc_run_module(openubmc_set_boot_device_module, module, log, report)


if __name__ == '__main__':
    main()
